package cn.zh.npro.Activity;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import cn.zh.npro.Adapter.ReAdapter;
import cn.zh.npro.Item.CaItem;
import cn.zh.npro.Item.ReItem;
import cn.zh.npro.MyApplication;
import cn.zh.npro.R;
import cn.zh.npro.Util.BaseUrl;
import cn.zh.npro.Util.ToastUtil;
import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.Response;

public class ReActivity extends BaseActivity {
   ImageView fil_back;
   TextView conlin;
   String  jas=null;
   MyApplication mapp;
   String clid="";
    ListView clist;
    ReAdapter reAdapter=null;
    ArrayList<CaItem> caItems=new ArrayList<CaItem>();

   EditText etname,etphon,etadre;
   CheckBox chbox;
   LinearLayout sulin;
    CaItem caItem=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_re);
        jas=getIntent().getStringExtra("ja");
        mapp= (MyApplication) getApplication();
        clid=mapp.getClID();
        initra();
       }


    @Override
    protected void onResume() {
        super.onResume();
        getCdent();
       }

    public void initra(){
        fil_back=findViewById(R.id.fil_back);
        chbox=findViewById(R.id.chbox);
        chbox.setChecked(false);
        conlin=findViewById(R.id.conlin);
        etname=findViewById(R.id.etname);
        etphon=findViewById(R.id.etphon);
        etadre=findViewById(R.id.etadre);
        sulin=findViewById(R.id.sulin);
        fil_back.setOnClickListener(this);
        conlin.setOnClickListener(this);
        sulin.setOnClickListener(this);
        clist=findViewById(R.id.clist);
        reAdapter=new ReAdapter(ReActivity.this,caItems);
        clist.setAdapter(reAdapter);
        clist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                etname.setText(caItems.get(i).getFullname());
                etphon.setText(caItems.get(i).getMobile());
                etadre.setText(caItems.get(i).getAddress());
                caItem=caItems.get(i);
            }
        });
        chbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b){
                   submit();
                }
            }
        });
       }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        int id=view.getId();
        switch (id){
            case R.id.fil_back:
                finish();
                break;
            case R.id.conlin:
                Intent intent=new Intent(ReActivity.this,ConnectAc.class);
                intent.putExtra("id",clid);
                startActivity(intent);
                break;
            case R.id.sulin:
                if (null==caItem){
                    final String name=etname.getText().toString();
                    final String phone=etphon.getText().toString();
                    final String adre=etadre.getText().toString();
                    if (name.equals("")||phone.equals("")||adre.equals("")){
                        ToastUtil.showLong(ReActivity.this,"信息不完整");
                        return;
                    }
                    caItem=new CaItem("",name,phone,adre,false);
                   }
                Intent intent1=new Intent(ReActivity.this,Order.class);
                intent1.putExtra("caItem",caItem);
                setResult(1,intent1);
                finish();
                break;
            }
        }


  //获取联系人
    public void getCdent(){
        OkHttpUtils.get(BaseUrl.GetCustomerDetail)
                .params("customerId",clid)
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                        try {
                            JSONObject js=new JSONObject(s);
                            boolean State=js.getBoolean("State");
                            if (State){
                                JSONObject Res=js.getJSONObject("Res");

                                JSONArray ja = Res.getJSONArray("LinkmanList");
                                caItems.clear();
                                for (int i=0;i<ja.length();i++){
                                    JSONObject ji=ja.getJSONObject(i);
                                    String linkmanid=ji.getString("linkmanid");
                                    String fullname=ji.getString("fullname");
                                    String mobile=ji.getString("mobile");
                                    String address=ji.getString("address");
                                    boolean isMaster=ji.getBoolean("isMaster");
                                    CaItem caItem=new CaItem(linkmanid,fullname,mobile,address,isMaster);
                                    caItems.add(caItem);
                                }
                                reAdapter.notifyDataSetChanged();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                    }
                });
           }







    //提交
    public void submit(){
        final String name=etname.getText().toString();
        final String phone=etphon.getText().toString();
        final String adre=etadre.getText().toString();
        if (name.equals("")){
            ToastUtil.showLong(ReActivity.this,"请填写姓名");
            return;
        }
        if (phone.equals("")){
            ToastUtil.showLong(ReActivity.this,"请填写手机号");
            return;
         }
        if (adre.equals("")){
            ToastUtil.showLong(ReActivity.this,"请填写地址");
            return;
         }
        ReItem reItem=new ReItem(false,phone,adre,name,clid,false,"");
        String str=new Gson().toJson(reItem);
        OkHttpUtils.post(BaseUrl.LinkmanEdit)
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .postJson(str)
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                        try {
                            JSONObject js=new JSONObject(s);
                            boolean State=js.getBoolean("State");
                            String Msg=js.getString("Msg");
                            if (State){
                                String LinkmanId=js.getString("LinkmanId");
                                caItem=new CaItem(LinkmanId,name,phone,adre,false);
                                ToastUtil.showLong(ReActivity.this,Msg);
                            }else {
                                ToastUtil.showLong(ReActivity.this,Msg);
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                    }
                });

             }


  }
